#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int n,m,k;
    cin>>n>>m>>k;
    vector<vector<char>> v(n+1,vector<char>(m+1));
    vector<vector<int>> w(n+1,vector<int>(m+1));
    int g=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>v[i][j];
            if(v[i][j]=='g')
            {
                g++;
                w[i][j]=1;
            }
        }
    }
    for (int i = 1; i <= n; i++) 
    {
		for (int j = 1; j <= m; j++) 
        {
			w[i][j] += w[i - 1][j] + w[i][j - 1] - w[i - 1][j - 1];
		}
	}
    int t=g;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(v[i][j]=='.')
            {
                t=min(t,
                    w[min(n,i + k - 1)][min(m,j + k - 1)] 
					- w[max(0ll,i - k)][min(m,j + k - 1)] 
                    - w[min(n,i + k - 1)][max(0ll,j - k)] 
                    + w[max(0ll,i - k)][max(0ll,j - k)]);
            }
        }
    }
    cout<<g-t<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}